package org.qydjk.h5.controller.customerCenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.qydjk.common.pojo.user.User;
import org.qydjk.common.pojo.user.UserDeliveryAddress;
import org.qydjk.common.pojo.user.UserExaminationPopulation;
import org.qydjk.common.util.StringUtil;
import org.qydjk.h5.controller.base.BaseController;
import org.qydjk.service.service.customerCenter.ICustomerAccountInfoService;
import org.qydjk.service.service.customerCenter.ICustomerCenterHomePageService;
import org.qydjk.service.service.customerCenter.ICustomerCouponService;
import org.qydjk.service.vo.UserCouponBeen;
import org.qydjk.service.vo.UserMoneyDetailBeen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Lanan on 2018/1/4 0004.
 */

@Controller
@RequestMapping("/customer/account")
public class CustomerAccountInfoController extends BaseController {

    @Autowired
    private ICustomerCouponService customerCouponService;
    @Autowired
    private ICustomerCenterHomePageService customerCenterHomePageService;
    @Autowired
    private ICustomerAccountInfoService customerAccountInfoService;


    private Integer rows = 10;

    /**
     * 余额
     * @return 页面地址
     */
    @RequestMapping("/balance.do")
    public String balance() {
        Integer userId = getUserId();
        try {
            BigDecimal money = customerAccountInfoService.getUserBalance(userId);
            request.setAttribute("balance", money);
            return "customer/balance";
        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }

    /**
     * 余额明细
     * @return 页面地址
     */
    @RequestMapping("/balanceDetail.do")
    public String balanceDetail() {
        return "customer/balanceDetail";
    }

    @RequestMapping("/balanceDetail/list.do")
    @ResponseBody
    public Object blanceDetailList(Integer page, Integer dataType) {
        Integer userId = getUserId();
        try {
            List<UserMoneyDetailBeen> userMoneyDetails = customerAccountInfoService.getUserMoneyDetails(userId, dataType, page, rows);
            return renderListSuccess(userMoneyDetails, rows);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /**
     * 交易明细
     * @return 页面地址
     */
    @RequestMapping("/tradeDetail.do")
    public String tradeDetail() {
        return "customer/tradeDetail";
    }

    @RequestMapping("/tradeDetail/list.do")
    @ResponseBody
    public Object tradeDetailList(Integer page) {
        return null;
    }

    /**
     * 优惠券
     * @return 页面地址
     */
    @RequestMapping("/coupon.do")
    public String coupon() {
        return "customer/coupon";
    }

    /**
     * 优惠券列表
     * @param page
     * @param dataType
     * @return
     */
    @RequestMapping("/coupon/list.do")
    @ResponseBody
    public Object couponList(Integer page, Integer dataType) {
        try {
            List<UserCouponBeen> userCoupons = customerCouponService.getUserCoupons(getUserId(), dataType, page, rows);
            return renderListSuccess(userCoupons, rows);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }


    /**
     * 账户信息
     * @return 页面地址
     */
    @RequestMapping("/info.do")
    public String accountInfo() {
        Integer userId = getUserId();
        try {
            User user = customerCenterHomePageService.getCustomerInfo(userId);
            request.setAttribute("user", user);
            String openId = user.getOpenid();
            String aliPayUserId = user.getAlipayUserId();
            if (StringUtil.isEmptyOrNull(openId)) {
                request.setAttribute("weChat", "0");
            } else {
                request.setAttribute("weChat", "1");
            }
            if (StringUtil.isEmptyOrNull(aliPayUserId)) {
                request.setAttribute("aliPay", "0");
            } else {
                request.setAttribute("aliPay", "1");
            }
            return "customer/accountInfo";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 个人信息
     * @return 页面地址
     */
    @RequestMapping("/personalInfo.do")
    public String personalInfo() {
        try {
            User user = customerCenterHomePageService.getCustomerInfo(getUserId());
            request.setAttribute("user", user);
            return "customer/personalInfo";
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 常用体检人
     * @return 页面地址
     */
    @RequestMapping("/contacts.do")
    public String contacts() {
        return "customer/contacts";
    }

    /**
     * 常用体检人列表
     * @param page 当前页
     * @return 数据
     */
    @RequestMapping("/contacts/list.do")
    @ResponseBody
    public Object contactsList(Integer page) {
        Integer userID = getUserId();
        try {
            List list = customerAccountInfoService.getUserExaminationPopulations(userID, page, rows);
            return renderListSuccess(list, rows);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /**
     * 常用体检人详情
     * @param id  常用体检人信息id
     * @param type 0 添加  1 修改
     * @return
     */
    @RequestMapping("/contactsDetail.do")
    public String contactsDetail(Integer id, Integer type) {
        request.setAttribute("type", type);
        if (type == 0) {
            return "customer/contactsDetail";
        }
        try {
            UserExaminationPopulation userExaminationPopulation = customerAccountInfoService.getUserExaminationPopulation(getUserId(), id);
            request.setAttribute("population", userExaminationPopulation);
            return "customer/contactsDetail";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用户常用体检人修改
     * @param userExaminationPopulation
     * @return
     */
    @RequestMapping("/update/contactsDetail.do")
    @ResponseBody
    public Object updateContactsDetail(UserExaminationPopulation userExaminationPopulation) {
        try {
            userExaminationPopulation.setUserId(getUserId());
            return customerAccountInfoService.updateUserExaminationPopulation(userExaminationPopulation);
        } catch (Exception e) {
            return fail(e);
        }
    }

    /**
     * 用户常用体检人添加
     * @param userExaminationPopulation
     * @return
     */
    @RequestMapping("/insert/contactsDetail.do")
    @ResponseBody
    public Object insertContactsDetail(UserExaminationPopulation userExaminationPopulation) {
        try {
            userExaminationPopulation.setUserId(getUserId());
            return customerAccountInfoService.insertUserExaminationPopulation(userExaminationPopulation);
        } catch (Exception e) {
            return fail(e);
        }
    }

    /**
     * 删除用户常用体检人
     * @param id
     * @return
     */
    @RequestMapping("/delete/contactsDetail.do")
    @ResponseBody
    public Object deleteContactsDetail(Integer id) {
        try {
            return customerAccountInfoService.deleteUserExaminationPopulation(id);
        } catch (Exception e) {
            return fail(e);
        }
    }

    private Object fail(Exception e) {
        e.printStackTrace();
        JSONObject resp = new JSONObject();
        resp.put("msg", e.getMessage());
        resp.put("success", false);
        return JSON.toJSON(resp);
    }




    /**
     * 领券中心
     * @return 页面地址
     */
    @RequestMapping("/couponCenter.do")
    public String couponCenter() {
        return "customer/couponCenter";
    }

    /**
     * 银行卡
     * @return 页面地址
     */
    @RequestMapping("/bankCard.do")
    public String bankCard() {
        return "customer/bankCard";
    }

    /**
     * 银行卡详情
     * @return 页面地址
     */
    @RequestMapping("/bankCardDetail.do")
    public String bankCardDetail() {
        return "customer/bankCardDetail";
    }

    /**
     * 银行卡新增或修改
     * @return 页面地址
     */
    @RequestMapping("/bankCardEdit.do")
    public String bankCardEdit() {
        return "customer/bankCardEdit";
    }

    /**
     * 支付密码
     * @return 页面地址
     */
    @RequestMapping("/payPwd.do")
    public String payPwd() {
        return "customer/payPwd";
    }

    /**
     * 设置支付密码
     * @return 页面地址
     */
    @RequestMapping("/setPayPwd.do")
    public String setPayPwd() {
        return "customer/setPayPwd";
    }

    /**
     * 修改支付密码
     * @return 页面地址
     */
    @RequestMapping("/editPayPwdByOld.do")
    public String editPayPwdByOld() {
        return "customer/editPayPwdByOld";
    }

    /**
     * 忘记支付密码
     * @return 页面地址
     */
    @RequestMapping("/editPayPwdByPhone.do")
    public String editPayPwdByPhone() {
        return "customer/editPayPwdByPhone";
    }

    /**
     * 设置新登录密码
     * @return 页面地址
     */
    @RequestMapping("/setPwd.do")
    public String setPwd() {
        return "customer/setPwd";
    }

    /**
     * 忘记登录密码
     * @return 页面地址
     */
    @RequestMapping("/editPwdByPhone.do")
    public String editPwdByPhone() {
        return "customer/editPwdByPhone";
    }

    /**
     * 修改登录密码
     * @return 页面地址
     */
    @RequestMapping("/editPwdByOld.do")
    public String editPwdByOld() {
        return "customer/editPwdByOld";
    }



    /*************************************------------- 地址相关 -------------*********************************/
    /**
     * 收货地址
     * @return 页面地址
     */
    @RequestMapping("/address.do")
    public String address() {
        return "customer/address";
    }

    /**
     * 收获地址列表
     * @return 列表
     */
    @RequestMapping("/address/list.do")
    @ResponseBody
    public Object addressList() {
        try {
            List<UserDeliveryAddress> userDeliveryAddresses = customerAccountInfoService.getUserDeliveryAddressList(getUserId());
            return renderSuccess(userDeliveryAddresses);
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /**
     * 修改用户默认地址
     * @param addressId
     * @return
     */
    @RequestMapping("/address/update/default.do")
    @ResponseBody
    public Object updateUserDefaultAddress(Integer addressId) {
        try {
            return customerAccountInfoService.updateUserDefaultDeliveryAddress(addressId);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e);
        }
    }

    /**
     * 删除用户收货地址
     * @param addressId
     * @return
     */
    @RequestMapping("/address/delete.do")
    @ResponseBody
    public Object deleteUserAddress(Integer addressId) {
        try {
            return customerAccountInfoService.deleteUserDeliveryAddress(addressId);
        } catch (Exception e) {
            return fail(e);
        }
    }

    /**
     * 插入收货地址
     * @param userDeliveryAddress
     * @return
     */
    @RequestMapping("/address/insert.do")
    @ResponseBody
    public Object insertUserAddress(UserDeliveryAddress userDeliveryAddress) {
        userDeliveryAddress.setUserId(getUserId());
        try {
            return customerAccountInfoService.insertOrUpdateUserDeliveryAddress(userDeliveryAddress, 0);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e);
        }
    }

    /**
     * 修改收货地址
     * @param userDeliveryAddress
     * @return
     */
    @RequestMapping("/address/update.do")
    @ResponseBody
    public Object updateUserAddress(UserDeliveryAddress userDeliveryAddress) {
        userDeliveryAddress.setUserId(getUserId());
        try {
            return customerAccountInfoService.insertOrUpdateUserDeliveryAddress(userDeliveryAddress, 1);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e);
        }
    }
}
